﻿/**********************************************
* Class : Validate
* Version : 1.0 
*______________________________________________
*
* Author : Wissarut Pimanmassuriya (Boat)
* Mobile : 081-8338186
* Email : wissarut@area80.net
* website : http://www.lifeztream.com/
*______________________________________________
*
* Usage : static class
* 
**********************************************/
package net.area80.validate {
		public class Validate {

			private static const NUMERIC_CHARS:String = "0123456789";
			private static const LEGAL_CHARS:String = "abcdefghijklmnopqrstuvwxyz"+NUMERIC_CHARS;
			private static const ALL_CHARS:String = LEGAL_CHARS+"-_";
			private static const ALLOWED_PHONE_CHARS:String = " -+()";
			public static const EMAIL_RESTRICT:String = "a-zA-Z0-9\\@\\-\\_\\.";

			//************************************************************//
			// PUBLIC EVENT METHODS
			//************************************************************//
			
			public static function email(strEmail:String):Boolean{


				strEmail = strEmail.toLowerCase();
				
				var iPos:Number = 0;
				var iCount:Number;
				var iAfterAt:Number = strEmail.indexOf("@")+1;
				var iLastDot:Number = strEmail.lastIndexOf(".");
				/* Number of characters allowed in domain postfix ('com', 'dk' etc.) */
				var iPostfix:Number = 4;

				if (strEmail.charAt(iPos) == "@" || iLastDot == -1) {
					return false;
				}

				while (iPos < iLastDot && strEmail.charAt(iPos) != "@") {

					if (iPos != 0 && strEmail.charAt(iPos++) != ".") {
						return false;
					}

					if (LEGAL_CHARS.indexOf(strEmail.charAt(iPos++)) == -1) {
						return false;
					}

					while(iPos < iLastDot && ALL_CHARS.indexOf(strEmail.charAt(iPos)) != -1) {
						iPos++;
					}

				}

				if (strEmail.charAt(iPos++) != "@" || strEmail.charAt(iPos) == ".") {
					return false;
				}

				while (iPos != iLastDot) {

					if (iPos != iAfterAt && strEmail.charAt(iPos++) != ".") {
						return false;
					}

					if (LEGAL_CHARS.indexOf(strEmail.charAt(iPos++)) == -1) {
						return false;
					}

					while(iPos != iLastDot && ALL_CHARS.indexOf(strEmail.charAt(iPos)) != -1) {
						iPos++;
					}

				}

				iPos++;

				for(iCount = 0; iPos < strEmail.length && LEGAL_CHARS.indexOf(strEmail.charAt(iPos)) != -1; iCount++) {
					iPos++;
				}

				return (iPos == strEmail.length && iCount >= 2 && iCount <= iPostfix);

			}
			
			
			public static function boolean(s:String):Boolean {
				return (s == "true" || s == "false");
			}
			
			
			public static function list(s:String, v:Array):Boolean{
				for(var i:uint=0; i<v.length; i++){
					if(v[i] == s){
						return true;
					}
				}
				return false;
			}
			
			public static function mobile (s:String):Boolean {
				if (s.length!= 10 || s.indexOf("08") != 0) return false;
				return true;
			}
			
			public static function numeric(s:String):Boolean { //contains only numeric characters

				if(s == ""){return false;}
				
				var iPos:Number = 0;
				while (iPos < s.length) {
					var charToCheck:String = s.charAt(iPos);
					if (NUMERIC_CHARS.indexOf(charToCheck) == -1) {
						return false;
					}
					
					iPos++;
				}
				return true;
			}
			
			public static function phone(s:String):Boolean{//contains only numeric characters," ","+","-","(",")", but not just a space or empty string
				if(typeof s != "string"){return false;} //since TYPE only counts in the Editor :~)
				if(s == " " || s == ""){return false;}
				
				var iPos:Number = 0;
				while (iPos < s.length) {
					var charToCheck:String = s.charAt(iPos);
					if (NUMERIC_CHARS.indexOf(charToCheck) == -1 && ALLOWED_PHONE_CHARS.indexOf(charToCheck) == -1) {
						return false;
					}

					iPos++;
				}
				return true;
			}
			public static function trim (s:String):String {
				var str:String = s;
				while (str.charAt(0) == " ") {
					str = str.substr(1);
				} 
				while (str.charAt(str.length-1) == " ") {
					str = str.substr(0,str.length-1);
				}
				return str;
			}
			public static function notempty(s:String):Boolean{
				return (trim(s) != "");
			}
			
			public static function fixedLength(s:String, aLengths:Array):Boolean{ //fks. [0,16,24] == a string of 0 or 16 or 24 characters length
				for(var i:uint=0; i<aLengths.length; i++){
					if(s.length == aLengths[i]){
						return true;
					}	
				}
				return false;
			}
			
			public static function betweenMinMaxLength(s:String, iMaxChars:Number, iMinChars:Number=0):Boolean{
					return !(s.length > iMaxChars || s.length < iMinChars);
			}
			
		}
}
